﻿using Prism.Events;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.Diagnostics;
using System.Linq;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows;
using Module.Common.Common;

namespace ServiceItem.Engine
{
    public static class XQEngine
    {

        /// <summary>
        /// 调用象棋引擎eleeye.exe，获取推荐走棋着法
        /// </summary>
        /// <param name="thinkTime">思考限制时间</param>
        /// <param name="depth">计算深度</param>
        /// <returns></returns>
        public static string CallEngine(int[,] qipan, bool SideTag, int thinkTime, int depth)
        {
            string fenstr = GlobalFunction.QiPanDataToFenStr(qipan, SideTag);
            Process proEngine = new()
            {
                StartInfo = new ProcessStartInfo()
                {
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    RedirectStandardInput = true,
                    CreateNoWindow = true,
                    FileName = "Engine/Engine/eleeye.exe"
                }
            };
            proEngine.Start();
            proEngine.StandardInput.WriteLine("ucci");
            proEngine.StandardInput.WriteLine("setoption batch on");
            proEngine.StandardInput.WriteLine("position fen " + fenstr);
            proEngine.StandardInput.WriteLine("go time " + thinkTime.ToString());
            proEngine.StandardInput.WriteLine("go depth " + depth.ToString());
            proEngine.StandardInput.WriteLine("quit");
            string output = proEngine.StandardOutput.ReadToEnd();
            proEngine.Close();
            if (!output.Contains("nobestmove"))
            {
                // 裁剪掉无用信息
                int start = output.IndexOf("info depth");
                output = output[start..];
                output = output.Replace("bye" + Environment.NewLine, "");
            }
            else
            {
                output = "No BestMove!";
            }
            return output;
        }

       
    }
}
